package com.jhgsys.internal.sypq.service.impl;

import com.jhgsys.internal.sypq.entity.*;
import com.jhgsys.internal.sypq.mapper.ProjectManagerMapper;
import com.jhgsys.internal.sypq.service.IProjectManagerService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 项目与经理对应表(一对多) Service实现
 *
 * @author ganjun
 * @date 2024-11-15 17:34:09
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectManagerServiceImpl extends ServiceImpl<ProjectManagerMapper, ProjectManager> implements IProjectManagerService {

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-15 17:34:09
     * @param request QueryRequest
     * @param projectManager projectManager
     * @return IPage<ProjectManager>
    */
    @Override
    public IPage<ProjectManager> findProjectManagerPage(QueryRequest request, ProjectManager projectManager) {
        LambdaQueryWrapper<ProjectManager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectManager::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<ProjectManager> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-15 17:34:09
     * @param projectManager projectManager
     * @return List<ProjectManager>
     */
    @Override
    public List<ProjectManager> findProjectManagerList(ProjectManager projectManager) {
	    LambdaQueryWrapper<ProjectManager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectManager::getDeletedFlg, 0);
        if(StringUtils.isNotEmpty(projectManager.getProjectCode())){
            queryWrapper.eq(ProjectManager::getProjectCode,projectManager.getProjectCode());
        }
        if(StringUtils.isNotEmpty(projectManager.getUserId())){
            queryWrapper.eq(ProjectManager::getUserId,projectManager.getUserId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author ganjun
    * @date 2024-11-15 17:34:09
    * @param id
    * @return ProjectManager
    */
    @Override
    public ProjectManager findProjectManagerById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author ganjun
     * @date 2024-11-15 17:34:09
     * @param projectManager projectManager
     */
    @Override
    @Transactional
    public boolean createProjectManager(ProjectManager projectManager) {
        return this.save(projectManager);
    }

    /**
     * 修改
     * @author ganjun
     * @date 2024-11-15 17:34:09
     * @param projectManager projectManager
     */
    @Override
    @Transactional
    public boolean updateProjectManager(ProjectManager projectManager) {
        return this.updateById(projectManager);
    }

    /**
     * 逻辑删除
     * @author ganjun
     * @date 2024-11-15 17:34:09
     * @param projectManager projectManager
     * @param projectManagerIds projectManagerIds
     */
    @Override
    @Transactional
    public boolean deleteProjectManager(ProjectManager projectManager,String projectManagerIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(projectManagerIds.split(StringPool.COMMA));
        projectManager.setDeletedBy(user.getUserName());
        projectManager.setDeletedTime(new Date());
        projectManager.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(projectManager,new LambdaQueryWrapper<ProjectManager>().in(ProjectManager::getId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author ganjun
    * @date 2024-11-15 17:34:09
    * @param projectManagerIds projectManagerIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteProjectManager(String projectManagerIds) {
        List<String> list = Arrays.asList(projectManagerIds.split(StringPool.COMMA));
        LambdaQueryWrapper<ProjectManager> wapper = new LambdaQueryWrapper<>();
        wapper.in(ProjectManager::getId,list);
        return this.remove(wapper);
    }

    @Override
    @Transactional
    public boolean physicallyDeleteByProject(String  projectCode) {
        LambdaQueryWrapper<ProjectManager> wapper = new LambdaQueryWrapper<>();
        wapper.eq(ProjectManager::getProjectCode,projectCode);
        return this.remove(wapper);
    }
}
